//
//  ByResetPwdViewController.m
//  Homept_ios
//
//  Created by mc on 16/9/6.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByResetPwdViewController.h"
#import "ByTextField.h"
#import "ByLoginViewModel.h"
#import "ByLoginVC.h"

@interface ByResetPwdViewController ()<RegisterDelegate,FindBackPswDelegate>
{
    ByTextField *_field;              //手机号输入框
    ByLoginViewModel *_viewmodel;
    BOOL _isShow;                     //判断密码是否显示
    UIImageView *_rightImg;           //右侧是否显示密码按钮
}

@end

@implementation ByResetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([_type isEqualToString:@"1"]) {
        self.title = @"设置密码";
    }else{
        self.title = @"重置密码";
    }
    _viewmodel = [[ByLoginViewModel alloc] init];
    _viewmodel.registerDelegate = self;
    _viewmodel.findBackPswDelegate = self;
    
    [self addLeftBackBarButtonItem];
    _isShow = NO;              //默认是不显示
    [self createViews];
}
#pragma mark ---创建视图
-(void)createViews{
    CGFloat x,y,width,height;
    x = 30;
    y = 20;
    width = SCREEN_WIDTH - 2*x;
    height = LineHeight;
    _field = [[ByTextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _field.secureTextEntry = YES;
    //左侧
    UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, LineHeight - 10, LineHeight - 10)];
    [leftImg setImage:[UIImage imageNamed:@"手机"]];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LineHeight, LineHeight)];
    [leftView addSubview:leftImg];
    _field.leftView = leftView;
    _field.leftViewMode = UITextFieldViewModeAlways;
    NSMutableAttributedString *paleceholderAttr = [[NSMutableAttributedString alloc] initWithString:@"设置新的登录密码"];
    [paleceholderAttr addAttribute:NSFontAttributeName value:FONT(13) range:NSMakeRange(0, paleceholderAttr.length)];
    [_field setAttributedPlaceholder:paleceholderAttr];
    [self.view addSubview:_field];
    //右侧
    _rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, LineHeight - 10, LineHeight - 10)];
    _rightImg.image = [UIImage imageNamed:@"yanjing"];
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LineHeight, LineHeight)];
    [rightView addSubview:_rightImg];
    _field.rightView = rightView;
    _field.rightViewMode = UITextFieldViewModeAlways;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
    [rightView addGestureRecognizer:tap];

    y = _field.bottom + 5;
    height = 1;
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    [self.view addSubview:line];
    
    y = line.bottom + 20;
    UIButton *loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, 35)];
    [loginBtn setTitle:@"完成并登录" forState:UIControlStateNormal];
    loginBtn.backgroundColor = HTB_THEME_COLOR;
    loginBtn.titleLabel.font = FONT(14);
    [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
}
//完成并登录
- (void)loginAction:(UIButton *)btn{
    if ([_field.text isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"密码不能为空"];
        return;
    }
    if ([_type integerValue] == 1) {
        ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
        model.reg_type = @"3";
        model.username = _phoneNumber;
        model.from = @"0";
        model.password = _field.text;
        [_viewmodel registe:model];
    }else if ([_type integerValue] == 2) {
        ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
        model.password = _field.text;
        model.mobile = _phoneNumber;
        [_viewmodel findBackPassword:model];

    }
    
}
//密码显示
- (void)click:(UITapGestureRecognizer *)tap {
    _isShow = !_isShow;
    if (_isShow) {
        _rightImg.image = [UIImage imageNamed:@"yanjing_no"];
        _field.secureTextEntry = NO;
    }else{
        _rightImg.image = [UIImage imageNamed:@"yanjing"];
        _field.secureTextEntry = YES;
    }
  
}
#pragma mark ---相关代理
- (void)registerSuccess:(ByNetBaseResult *)result{
    NSLog(@"%@",result.data);
    [ByCoreToolAlert showSuccessWithText:result.msg];
    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:_field.text,@"psw",_phoneNumber,@"phone", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"registermodel" object:nil userInfo:dic];
    [self by_normalPopVC:@"ByRegisterVC"];
    
}
- (void)registerFail:(ByNetBaseResult *)result{
    NSLog(@"%@",result.msg);
    [ByCoreToolAlert showFailureWithText:result.msg];
}


- (void)findBackPswSuccess:(ByNetBaseResult *)result {
    [ByCoreToolAlert showSuccessWithText:result.msg];
    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:_field.text,@"psw",_phoneNumber,@"phone", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"findpswmodel" object:nil userInfo:dic];
    [self by_normalPopVC:@"ByForgetPwdVC"];
}

- (void)findBackPswFail:(ByNetBaseResult *)result {
    [ByCoreToolAlert showFailureWithText:result.msg];
}


@end
